/***************************************************************************
Name:		chathandler.cpp
Autor:		Kevin Huber
Version:	0.1 Alpha
Last Edit:      12.06.2009

Beschreibung:
Diese Klasse behandelt alle eingehenden Pakete
***************************************************************************/

#include "chathandler.h"

ChatHandler::ChatHandler(QObject *parent) : QObject(parent)
{
}

ChatHandler::~ChatHandler()
{
}

bool ChatHandler::handlePackage(Package *package)
{
    if( package->typ == TUNNEL_PACKAGETYP_MESSAGE )
    {
        handleNewMessage(package->message);

        return true;
    }
    else if( package->typ == TUNNEL_PACKAGETYP_SYSTEM )
    {
        handleSystemCommand(package->commandCategory, package->command, package->arguments);

        return true;
    }

    return false;
}

void ChatHandler::handleNewMessage(const QString &message)
{
    if( message == "" )
        return;

    emit newMessage(message);
}

void ChatHandler::handleSystemCommand(const quint16 &category, const quint16 &command, const QString &arguments)
{
    if( category == TUNNEL_COMMANDCATEGORY_COMM )
    {
        if( command == TUNNEL_COMMAND_AUTH )
        {
            emit authCommand(arguments);
        }
        else
            emit error("Unknown Comm Command: "+command);
    }
    else if( category == TUNNEL_COMMANDCATEGORY_FRIENDS )
    {
        if( command == TUNNEL_COMMAND_UPDATEFRIENDLIST )
        {
            emit getFriendlist();
        }
        else if( command == TUNNEL_COMMAND_ADDFRIEND )
        {
            emit addFriend(arguments);
        }
        else if( command == TUNNEL_COMMAND_GETFRIENDREQUESTS )
        {
            emit getFriendrequests();
        }
        else if( command == TUNNEL_COMMAND_ACCEPTFRIENDINVENTATION )
        {
            emit acceptFriendInventation(arguments.toULongLong());
        }
        else
            emit error("Unknown Friends Command: "+command);
    }
    else
        emit error("Unknown System Command Categorie: "+category);
}
